
local layer = nil
function LoadingTutorial(mode, func, stage)
    layer = CCLayer:create()
    
    
    
    
    ---资源加载
    local _lb_loading = nil
    local _onBeging = nil
    
    ---------------------------
    local _loadingEnd = false
    local _loadingSums = 0 --加载总数
    local _loadingFine = 0 --完成数量
    local _joinGame = false
    local _baseLayer_entry = nil
    
    ---- 加载资源内容
    ResourcesTalbes = {
        {rtype=Res_Plist,   d1=GetPathRes("bom_normal.plist"),d2=GetPathRes("bom_normal.png"),},
        {rtype=Res_Plist,   d1=GetPathRes("fight_bg.plist"),d2=GetPathRes("fight_bg.png"),},
        {rtype=Res_Plist,   d1=GetPathRes("skills.plist"),d2=GetPathRes("skills.png"),},
        {rtype=Res_Plist,   d1=GetPathRes("gameUI2.plist"),d2=GetPathRes("gameUI2.png"),},
        {rtype=Res_Plist,   d1=GetPathRes("gameUI3.plist"),d2=GetPathRes("gameUI3.png"),},
    }
    
    local function loading_next()
        if _loadingFine >= #ResourcesTalbes then
            _onBeging()
            return
        end
        
        _loadingFine = _loadingFine + 1
        local _nowload = ResourcesTalbes[_loadingFine]
        
        if _nowload.rtype == Res_Plist then
            CCSpriteFrameCache:sharedSpriteFrameCache():addSpriteFramesWithFile(_nowload.d1, _nowload.d2)
        elseif _nowload.rtype == Res_Png then
            CCTextureCache:sharedTextureCache():addImage(_nowload.d1)
        elseif _nowload.rtype == Res_Bgm then
            SimpleAudioEngine:sharedEngine():preloadBackgroundMusic(string.format("%s%s",sFilePath_Sound, _nowload.d1))
        elseif _nowload.rtype == Res_Effs then
            SimpleAudioEngine:sharedEngine():preloadEffect(string.format("%s%s",sFilePath_Sound, _nowload.d1))
        end
        
        _lb_loading:setString(table.concat({"等待读取中... ",string.format("%.0f", (_loadingFine/#ResourcesTalbes)*100),"%"}))
    end
    
    local _frame = 0
    local function update()
        
        --print("load...",_frame)
        --print(_frame)
        -- 每次加载一个资源
        if _loadingEnd then
            return
        end
        
        if _frame == 0 then
            -- 加载音效音乐
            --Init_GameSound()
        end
        
        loading_next()
        _frame = _frame + 1
    end
    
    local function SpriteEase_onEnterOrExit(tag,tag2)
        if not _joinGame then
            _joinGame = true
            _baseLayer_entry = CCDirector:sharedDirector():getScheduler():scheduleScriptFunc(update,1.0/60.0,false)
        else
            _joinGame = false
            if _baseLayer_entry ~= nil then
                CCDirector:sharedDirector():getScheduler():unscheduleScriptEntry(_baseLayer_entry)
            end
            _baseLayer_entry = nil
        end
    end
    
    
    -- 背景
    ---显示不同的提示
    ---显示不同的过关目标
    local stag = nil
    ---根据不同的模式,加载不同的教程背景
    local sfile = "Tt_Hunjing.png"
    local smessage = "在游戏里面消除砖块可以获得魂晶\n魂晶可用于升级技能,很重要哦"
    if mode == UI_HLWS_Game then
        sfile = "Tt_Hunjing.png"
        smessage = "在游戏里面消除砖块可以获得魂晶\n魂晶可用于升级技能,很重要哦"
        
        stag = string.format("通关条件：积分达到 %d", StageInfos_HLWS[stage][PScore])
    elseif mode == UI_SJSK_Game then
        sfile = "Tt_Shuijing.png"
        smessage = "在游戏里面消除砖块可以获得金币\n金币可以用于装备和道具的购买"
    elseif mode == UI_ZJWG_Game then
        sfile = "Tt_Tiaozhan.png"
        smessage = "点击三个以上相邻砖块消除"
        
        local _AllMonsterSums = 0
        local _monsterLastSums = 0
        if Stage_Infos_ZJWG[stage][PMonster] ~= nil then
            for k,mid in pairs(Stage_Infos_ZJWG[stage][PMonster]) do
                --数量
                local cut = 1
                if Stage_Infos_ZJWG[stage][PCount] ~= nil and Stage_Infos_ZJWG[stage][PCount][k] ~= nil then
                    cut = Stage_Infos_ZJWG[stage][PCount][k]
                end
                for i=1,cut do
                    _monsterLastSums = _monsterLastSums + 1
                end
            end
            
            _AllMonsterSums = _monsterLastSums
        end
        stag = string.format("通关条件：击杀 %d 名敌人。", _AllMonsterSums)
    elseif mode == UI_WXHL_Game then
        sfile = "Tt_Tiaozhan.png"
        smessage = "点击三个以上相邻砖块消除"
    end
    
    --背景
    local _bgSpr = nil
    _bgSpr = CCSprite:createWithSpriteFrameName(s_Space_bg)
    _bgSpr:setPosition(gdccp(DWinSizeWidth/2, DWinSizeHeight/2))
    layer:addChild(_bgSpr)
    _bgSpr = CCSprite:createWithSpriteFrameName(s_Space_bgHead)
    _bgSpr:setPosition(gdccp(DWinSizeWidth/2, 960-32))
    layer:addChild(_bgSpr, 9)
    
    --目标
    if stag ~= nil then
        --local txtTag = CCLabelTTF:create(stag, "Arial", FontSize+8)
        ----txtTag:setColor(ccc3(96,163,255))
        --txtTag:setPosition(DWinSizeWidth/2, DWinSizeHeight-100)
        --layer:addChild(txtTag)
    end
    
    --教程
    _bgSpr = CCSprite:createWithSpriteFrameName(sfile)
    _bgSpr:setPosition(gdccp(DWinSizeWidth/2, DWinSizeHeight/2+100))
    layer:addChild(_bgSpr)
    
    local txtMsg = CCLabelTTF:create(smessage, "Arial", FontSize6)
    txtMsg:setColor(ccc3(96,163,255))
    txtMsg:setAnchorPoint(ccp(0,1))
    txtMsg:setPosition(gdccp(100, 300))
    layer:addChild(txtMsg)
    
    
    _lb_loading = CCLabelTTF:create("等待读取中...", "Arial", FontSize4)
    _lb_loading:setColor(ccc3(112,233,255))
    _lb_loading:setAnchorPoint(ccp(0,1))
    _lb_loading:setPosition(gdccp(400, 120))
    
    _onBeging = function()
        _lb_loading:setPosition(gdccp(450, 120))
        _lb_loading:setString("点击开始")
        _lb_loading:setColor(ccc3(255,255,255))
        
        local array = CCArray:create()
        array:addObject(CCFadeTo:create(1.0, 50))
        array:addObject(CCFadeTo:create(1.0, 255))
        _lb_loading:runAction(CCRepeatForever:create(CCSequence:create(array)))
        
        print(CCTextureCache:sharedTextureCache():dumpCachedTextureInfo())
        _loadingEnd = true
    end
    
    layer:addChild(_lb_loading)
    
    
    ---
    local function onTouch(eventType, x, y)
        if _loadingEnd then
            func(stage)
        end
    end
    layer:setTouchEnabled(true)
    layer:registerScriptTouchHandler(onTouch)
    
    layer:registerScriptHandler(SpriteEase_onEnterOrExit)

    
    local scene = CCScene:create()
    scene:addChild(layer)
    CCDirector:sharedDirector():replaceScene(scene)
end





local TutorialTables = {
    [UI_HLWS_Game] = {
        {a="tur_item.png", b="por_item.png", c={{x=120,y=60,}, {x=320,y=60,}, {x=520,y=60,}}, x = 80, y = 180},
        {a="tur_pro.png", b="por_pro.png", c={{x=320,y=810,}}, x = 250, y = 700, f = true},
        {a="pro_time.png", b="por_time.png", c={{x=500,y=920,}}, x = 450, y = 800, f = true},
        {a="tur_hunj.png", b="port_hunj.png", c={{x=200,y=885,}}, x = 80, y = 780, f = true},
    },
    [UI_SJSK_Game] = {
        {a="tur_item.png", b="por_item.png", c={{x=220,y=60,}, {x=420,y=60,}}, x = 180, y = 180},
        {a="tur_pro.png", b="por_pro.png", c={{x=320,y=810,}}, x = 250, y = 700, f = true},
        {a="tur_shuijing.png", b="port_hunj.png", c={{x=450,y=917,}}, x = 400, y = 810, f = true},
    },
    [UI_ZJWG_Game] = {
        {a="tur_item.png", b="por_item.png", c={{x=170,y=75,},{x=320,y=75,},{x=470,y=75,}}, x = 220, y = 180},
        {a="tur_pro.png", b="por_pro.png", c={{x=320,y=788,}}, x = 250, y = 690, f = true},
        {a="tur_shangdi.png", b="port_hunj.png", c={{x=320,y=935,}}, x = 200, y = 840, f = true},
    },
}
--教程
Func_ShowGameTutorial = function(key, func, scene)
    local layer = CCLayer:create()
    
    --是否需要教程
    if GetFirstTutorial(key) == 1 then
        func()
        return
    end
    
    
    local function endTutorial()
        Play_Effect(sme_Click, nil, true)
        
        --触摸关闭
        CCDirector:sharedDirector():getRunningScene():removeChild(layer, true)
        
        SetFirstTutorial(key)
        
        func()
    end
    
    local tutinfos = TutorialTables[key]
    
    -- 当前加载的tutor索引
    local tutorIndex = 1
    
    local _parent = nil
    local showNexttutor = nil
    local showAlls = false
    showNexttutor = function()
        --if not showAlls and _parent ~= nil then
        --    layer:removeChild(_parent, true)
        --    _parent = nil
        --end
        
        if tutorIndex > #tutinfos then
            --显示所有
            --if showAlls == false then
            --    showAlls = true
            --    tutorIndex = 1
            --    for i=1,#tutinfos do
            --        showNexttutor()
            --    end
            --else
                endTutorial()
            --end
            return
        end
        
        if showAlls then
            Play_Effect(string.format("smCombo4.mp3"))
        else
            Play_Effect(string.format("smCombo%d.mp3",tutorIndex))
        end
        
        _parent = CCNode:create()
        layer:addChild(_parent)
        --
        local infos = tutinfos[tutorIndex]
        
        --140, 115
        local ofpx, ofpy = 140 , 115
        local _parentSp = CCNode:create()
        _parent:addChild(_parentSp)
        --背景
        
        local txtdialog = CCSprite:createWithSpriteFrameName("tur_dialog.png")
        local ttp = -15
        if infos.f then
            ttp = ttp * -1
            ofpy = ofpy * -1
            txtdialog:setFlipY(true)
        end
        txtdialog:setPosition(gdccp(0 - ofpx,ttp + ofpy))
        _parentSp:addChild(txtdialog)
        --文字
        local txtSp = CCSprite:createWithSpriteFrameName(infos.a)
        txtSp:setPosition(gdccp(0 - ofpx,ofpy))
        _parentSp:addChild(txtSp)
        
        _parentSp:setPosition(gdccp(infos.x + ofpx, infos.y - ofpy))
        --方框
        for k, _pos in pairs(infos.c) do
            local txt = CCSprite:createWithSpriteFrameName(infos.b)
            txt:setPosition(gdccp(_pos.x, _pos.y))
            _parent:addChild(txt)
        end
        
        
        tutorIndex = tutorIndex + 1
        
        
        _parentSp:setScale(0.1)
        local array = CCArray:create()
        array:addObject(CCScaleTo:create(0.3, 1.0))
        --array:addObject(CCFadeIn:create(0.5))
        --array:addObject(CCFadeTo:create(1.0, 255))
        --array:addObject(CCFadeOut:create(0.5))
        _parentSp:runAction(CCSequence:createWithTwoActions(CCSequence:create(array), CCCallFuncN:create(function()
            --显示下一个
            --showNexttutor()
        end)))
    end
    
    
    local touchBegin = function(x,y) --触摸
        --Play_Effect(sme_Click, nil, true)
        
        showNexttutor()
    end
    
    local layerBg = GFunc_CreateLayerEnterOrExit(ccc4(0,0,0,100),
        nil, nil, nil,touchBegin, nil, nil,
        nil, true)
    layer:addChild(layerBg)
    
    local spsp = CCSprite:createWithSpriteFrameName("por_speed.png")
    spsp:setPosition(gdccp(600, 50))
    local array = CCArray:create()
    array:addObject(CCScaleTo:create(0.4, 1.3))
    array:addObject(CCScaleTo:create(0.4, 1.0))
    spsp:runAction(CCRepeatForever:create(CCSequence:create(array)))
    layer:addChild(spsp)
    
    showNexttutor()
    
    scene:addChild(layer)
end

























